<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-08-02
 * Time: 14:29
 */

namespace App\Repositories;


use App\Models\User;
use App\Models\UserLoginLog;

/**
 * Class UserLoginLogRepository
 * @package App\Repositories\Admin
 */
class UserLoginLogRepository
{
    /**
     * @var UserLoginLog 注入的UserLoginLog model
     */
    protected $user_login_log;

    /**
     * @var User 注入的User model
     */
    protected $user;

    /**
     * UserLoginLogRepository constructor.
     * @param UserLoginLog $userLoginLog
     * @param User $user
     */
    public function __construct(UserLoginLog $userLoginLog, User $user){
        $this->user_login_log = $userLoginLog;
        $this->user = $user;
    }

    /**
     * 插入登陆记录表
     * @param array $data
     * @return mixed
     */
    public function userLogin(array $data){
        return $this->user_login_log->insert($data);
    }

    /**
     * 返回用户登录列表
     * @param array $data
     * @param $limit
     * @return mixed
     */
    public function getUserLoginLogPage(array $data, $limit){
        return $this->user_login_log
            ->with(['user'])
            ->when($data['user'], function ($q)use($data){
                $q->where($data['user']);
            })
            ->when($data['time'], function ($q)use($data){
                $q->where($data['time']);
            })
            ->orderByDesc('login_time')
            ->paginate($limit);
    }
}